//登录模块





var loginApp = angular.module('loginApp',[]);
loginApp.controller('loginController',function($scope,$http){
    $scope.formData = {};
    $scope.postForm = function(){
        $scope.formData.action = 'login';
        $http({
            method:'POST',//发送方式
            url:'./get.php',//发送地址
            data:$.param($scope.formData),//发送的数据
            headers:{'Content-Type':'application/x-www-form-urlencoded'}//请求头
        }).success(function(data){
            console.log(data);
            //对数据的正确判断
            if(!data.success){
                if(!data.errors){
                    $scope.message = data.message;
                }else{
                    $scope.errorUsername = data.errors.username;
                    $scope.errorPassword = data.errors.password;
                }
            }else{
                //当密码正确的时候，跳转到列表页面
                window.location.href = '#/0';
            }

        })
    }
})
//列表模块
var pageList = angular.module('pageList',[]);
pageList.directive('clock',function(){
    return {
        restrict:'E',
        replace:true,
        template:"<div class='clock'></div>",
        link:function(scope,element,attrs){
            //将当前的时间输出到视图当中去
             var times = function() {
                 //创建当前的时间
                 var time = new Date();
                 var year = time.getFullYear();
                 var month = time.getMonth() + 1;
                 var day = time.getDate();
                 var hour = time.getHours() ;
                 var seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
                 var minutes = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes() ;
                 var timer = '当前时间:' + year + '-' + month + '-' + day + '   ' + hour + ':' + minutes + ':' + seconds;
                 element.text(timer);
                 setTimeout(times,1000);
             }
            times();

        }
    }
})
pageList.controller('ListTypeCtrl',function($scope,$http){
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function(data){
        $scope.lists = data;
    }).error(function(data){
        console.log('error...')
    })
    $scope.toppp = function(){
        $('body').animate({scrollTop:0},500)
    }
    $scope.bottt = function(){
        $('body').animate({scrollTop:document.body.clientHeight+'px'},500)
    }
})
//文章页面的列表
//创建arclistCtrl控制器拿到所有的文章
 total = 0;//文章总数量
pageList.controller('arcListCtrl',function($scope,$http,$location,$stateParams){
    
    $scope.typeid = $location.path().replace('/','');
    
    if($scope.typeid == 0){
        $get_total_url = 'get.php?action=get_total'
    }else{
        $get_total_url = 'get.php?action=get_total&where=typeid=' + $scope.typeid
    }
    
    $http({
        method:"GET",
        url:$get_total_url
    }).success(function(data){
        $scope.paginationConf.totalItems = data.total;
    }).error(function(data){
        console.log('error....')
    })
    
    $scope.paginationConf = {
       
        currentPage: 1,
        itemsPerPage: 5,
        pagesLength: 5,
        perPageOptions: [5, 10, 20, 30, 40, 50],
        rememberPerPage: 'perPageItems',
        onChange: function () {
            //获取分页的开始数
            if ($scope.paginationConf.currentPage == 1) {
                $scope.limit = 0;
            } else {
                //第一页的话,0 ,5 第二页 5,10,第三页 10,15
                $scope.limit = $scope.paginationConf.currentPage *
                    $scope.paginationConf.itemsPerPage -
                    $scope.paginationConf.itemsPerPage
            }
          
            if ($scope.typeid == 0) {
                $get_url = 'get.php?action=get_list&offset=' + $scope.limit + '&rows=' + $scope.paginationConf.itemsPerPage + '&orderField=id&orderBy=DESC';
            } else {
                $get_url = 'get.php?action=get_list&offset=' + $scope.limit + '&rows=' + $scope.paginationConf.itemsPerPage + '&where=typeid=' + $scope.typeid + '&orderField=id&orderBy=DESC';
            }
           
            $http({
                method: 'GET',
                url: $get_url
            }).success(function (data) {
                $scope.lists = data;
            }).error(function (data) {
                console.log('error...')
            })
        }
    }
    //删除开始
    $scope.del = function(index,Id){
        //这里是删除开始的地方
        //删除DOM
        $scope.lists.splice(index,1);
        //获取Id进行删除
        var gets = "?action=delete_article&id="+Id;
        $http({
            method:"GET",
            url:'get.php' + gets
        }).success(function(data) {
            console.log(data);
            if (data.code==101) {
                //删除成功
                console.log('删除成功');
                $scope.meg_success="删除成功！";
                $("#successbox").animate({opacity:'1'}).addClass("slideDown");
                setTimeout(function(){$("#successbox").removeClass().animate({opacity:'0'});}, 800);
                $scope.paginationConf.totalItems --;
            } else {
                //删除失败
                console.log('删除失败');
                $scope.meg_success="";
                $scope.meg_error="删除失败! ";
                $("#errorbox").animate({opacity:'1'}).addClass("slideDown");
                setTimeout(function(){$("#errorbox").removeClass().animate({opacity:'0'});}, 800);
            }
        })
    }
    

})

//新增
var addCont = angular.module('addCont',[]);
addCont.controller('addContCtrl',function($scope,$http){
    //第一步首先获取分类的列表
    $http({
        method:'GET',
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function(data){
        $scope.lists = data;
    }).error(function(data){
        console.log('error...')
    })

    //第二步，执行写入步骤
    $scope.formData = {};
    $scope.formData.action = 'add_article';
    $scope.postForm = function(){
        $http({
            method:'POST',
            url:'get.php',
            data:$.param($scope.formData),
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function(data){
            $scope.errorBye = function(){
                $('#errorbox').fadeOut();
            }
            $scope.errorShow = function(){
                $('#errorbox').fadeIn();
            }
            if(!data.errors){
                $scope.meg_success = '插入成功! 正在返回列表页面';
                $scope.meg_error = '';
                setTimeout(function(){
                    location.href = '#/0'
                },1000)
            }else{
                //失败
                $scope.meg_success = '';
                var get_error = '';
                if(data.errors.hasOwnProperty('title')){
                    get_error = data.errors.title;
                }
                if(data.errors.hasOwnProperty('content')){
                    get_error = data.errors.content;
                }
                if(data.errors.hasOwnProperty('typeid')){
                    get_error = data.errors.typeid;
                }
                $scope.meg_error = get_error;
            }
        })
    }
})

//修改模块
var modifyCont = angular.module('modifyCont',[]);
modifyCont.controller('modifyContCtrl',function($stateParams,$scope,$http){
    //获取分类
    $http({
        method:"GET",
        url:'get.php?action=get_arctype&where=reid=0'
    }).success(function(data){
        $scope.types = data;
    })
    //读取当前文章信息
    $http({
        method:"GET",
        url:'get.php?action=get_article&id=' + $stateParams.Id
    }).success(function(data){
        $scope.lists = data;
    }).error(function(data){
        console.log('error...')
    })
    //更新数据
    $scope.formData = {};
    $scope.postForm = function(){
        $scope.formData.action = 'update_article';
        $scope.formData.id = $stateParams.Id;
        $scope.formData.title = form.title.value;
        $scope.formData.content = form.content.value;
        $scope.formData.typeid = $('#typeid option:selected').val();
        $scope.formData.typename = $('#typeid option:selected').val();
        //发送数据
        $http({
            method:'POST',
            url:'get.php',
            data:$.param($scope.formData),
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function(data){
            console.log(data);
            if(data.code == 101){
                $scope.meg_success = '修改成功';
                $scope.meg_error = '';
                setTimeout(function(){location.href = '#/0'},1000)
            }else{
                $scope.meg_success = '';
                var get_error = '';
                $scope.errorBye = function(){
                    $('#errorbox').fadeOut();
                }
                $scope.errorShow = function(){
                    $('#errorbox').fadeIn();
                }
                if(data.errors){
                    if(data.errors.hasOwnProperty('title')){
                        get_error = data.errors.title;
                    }
                    if(data.errors.hasOwnProperty('content')){
                        gat_error = data.errors.content;
                    }
                    $scope.meg_error = get_error;
                }else{
                    $scope.meg_error = '修改失败，无任何改动';
                }
            }
        })
    }
})

//查询
var showCont = angular.module('showCont',[]);
showCont.controller('showContCtrl',function($scope,$http,$stateParams){
    $http({
        method:"GET",
        url:'get.php?action=get_article&id=' + $stateParams.Id
    }).success(function(data){
        $scope.lists = data;
    }).error(function(data){
        console.log('error....')
    })
})


